package com.example.leetcode.trainingcamp.week1.practice2;

/**
 * 字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。
 */
public class Offer01_05 {


    public boolean oneEditAway(String first, String second) {
        int firstLen = first.length();
        int secondLen = second.length();
        int abs = Math.abs(firstLen - secondLen);
        if (abs>1){
            return false;
        }
        if (firstLen == 1 && secondLen==1){
            return true;
        }
        int temp = -1 ;
        int min = Math.min(firstLen, secondLen);
        for (int i = 0 ;i<min;i++){
            if (first.charAt(i) != second.charAt(i)){
                temp = i;
                break;
            }
        }
        if (temp == -1){
            return true;
        }else {
            if (firstLen>secondLen){
                String s = first.substring(0,temp)+ first.substring(temp+1,firstLen);
                return s.equals(second);
            }else if (firstLen<secondLen){
                String s = second.substring(0,temp)+second.substring(temp+1,secondLen);
                return s.equals(first);
            }else {
                first = first.substring(0,temp)+second.charAt(temp)+first.substring(temp+1,secondLen);
                System.out.println(first);
                return first.equals(second);
            }
        }
    }
}



class TestOffer01_05{
  public static void main(String[] args) {
    Offer01_05 o = new Offer01_05();
    String first = "intention";
    String second = "execution";
    System.out.println(first.substring(0,1));
    System.out.println(o.oneEditAway(first,second));
  }
}